package com.cbtx.module_sp.ui.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import com.cbtx.module.common.bean.AreaItem
import com.cbtx.module.common.vm.LoginVM
import com.cbtx.module_sp.MyApplication
import com.cbtx.module_sp.R
import com.cbtx.module_sp.TaskService
import com.cbtx.module_sp.ui.activity.base.MyBaseVMActivity
import com.txcb.lib.base.utils.LoadingDialogUtil
import com.txcb.lib.base.utils.LogUtil
import com.txcb.lib.base.utils.ToastUtil
import kotlinx.android.synthetic.main.sp_activity_login.*

/**
 * 个人投放直接登录
 */
class LoginActivity : MyBaseVMActivity<LoginVM>() {

    companion object {
        fun start(activity: Activity?, loginType: String) {
            val intent = Intent(activity, LoginActivity::class.java)
            intent.putExtra("loginType", loginType)
            activity?.startActivity(intent)
        }

        fun start(activity: Activity?, loginType: String, launchAreaId: Int) {
            val intent = Intent(activity, LoginActivity::class.java)
            intent.putExtra("loginType", loginType)
            intent.putExtra("launchAreaId", launchAreaId)
            activity?.startActivity(intent)
        }
    }

    override fun getPresenterClazz(): Class<LoginVM> {
        return LoginVM::class.java
    }

    override fun getLayoutId(): Int {

        return R.layout.sp_activity_login
    }

    override fun initView(savedInstanceState: Bundle?) {

        mVM.getIntent(intent)
        mTvCode.text = "云屏编码:${MyApplication.mAndroidId}"
        addClickListener(mTvBack)
        addClickListener(mTvLogin)

        if (mVM.isNeedInputMarker) {
            mEdtMarkers.visibility = View.VISIBLE
            mIvMarkers.visibility = View.VISIBLE

            mEdtPhone.visibility = View.GONE
            mIvPhone.visibility = View.GONE

            mEdtAccount.visibility = View.GONE
            mIvAccount.visibility = View.GONE

        }

        //登录结果
        mVM.loginResult.observe(this, Observer {
            LoadingDialogUtil.closeDialog()
            //登录成功
            if (it) {
                MainActivity.start(mActivity!!)
            }
        })
        mTouchView.setOnTouchListener { view, motionEvent ->
            LogUtil.d("mTouchView")
            TaskService.activityTouchTime = System.currentTimeMillis()
            checkTouch(motionEvent)
            return@setOnTouchListener false
        }

        mTouchLongView.setOnLongClickListener {
            toNetSet()
            return@setOnLongClickListener false
        }

    }


    override fun onMyViewClick(view: View) {

//        if (view.id == R.id.mTvBack) {
//            finish()
//        } else if (view.id == R.id.mTvLogin) {
//            val loginMemberNo = mEdtAccount.text.toString().trim()
//            val loginPhone = mEdtPhone.text.toString().trim()
//            val marker = mEdtMarkers.text.toString().trim()
//
//            if (!mVM.isNeedInputMarker) {
//                if (TextUtils.isEmpty(loginMemberNo)) {
//                    ToastUtil.showToast("请输入伯乐号")
//                    return
//                }
//
//                if (TextUtils.isEmpty(loginPhone)) {
//                    ToastUtil.showToast("请输入手机号")
//                    return
//                }
//            } else {
//                if (TextUtils.isEmpty(marker)) {
//                    ToastUtil.showToast("请输入备注")
//                    return
//                }
//            }
//            LoadingDialogUtil.showDialog(mActivity, "登录中")
//            mVM.loginRq(loginMemberNo, loginPhone, marker)
//        }
    }

    override fun getData() {

    }
}